
//https://leetcode.cn/problems/reverse-words-in-a-string-iii/
void Swap(char* p1, char* p2)
{
    char tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

void reverseString(char* s, int sSize) {
    int begin = 0;
    int end = sSize - 1;
    while (begin < end)
    {
        Swap(&s[begin], &s[end]);
        ++begin;
        --end;
    }

}

char* reverseWords(char* s)
{
    int len = strlen(s);
    int count = 0;
    int n = 0;
    while (s[n])
    {
        if (s[n] == ' ')
        {
            reverseString(s + n - count, count);
            count = 0;
        }
        else
        {
            ++count;
        }
        ++n;
    }
    reverseString(s + n - count, count);
    return s;
}